{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "369ad922",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "import lightgbm as lgb\n",
    "\n",
    "from sklearn.model_selection import KFold\n",
    "from sklearn.metrics import f1_score\n",
    "from sklearn.metrics import roc_auc_score\n",
    "from collections import Counter\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "import pre_process\n",
    "from sklearn.model_selection  import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d3f7a821",
   "metadata": {},
   "outputs": [],
   "source": [
    "#读取相关文件\n",
    "train_path = '../data/train.csv'\n",
    "test_path = '../data/test.csv'\n",
    "submit_path = '../data/车辆贷款违约预测挑战赛sample_submit.csv'\n",
    "train_data = pd.read_csv(train_path)\n",
    "test_data = pd.read_csv(test_path)\n",
    "submit_data = pd.read_csv(submit_path)\n",
    "#生成提交文件\n",
    "submit_data['customer_id'] = test_data['customer_id']\n",
    "submit_data['loan_default'] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "63171383",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_data,test_data = pre_process.fill_inf(train_data,test_data)#填补inf值\n",
    "train_data,test_data = pre_process.del_singular_feature(train_data,test_data)#删除单值属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "90880af9",
   "metadata": {},
   "outputs": [],
   "source": [
    "#划分数据集\n",
    "labels = train_data['loan_default'].values\n",
    "features_scaler = train_data.drop(['loan_default'],axis=1).values\n",
    "X_train,X_test, y_train, y_test = train_test_split(features_scaler, labels, test_size = 0.2, random_state = 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "719075a3",
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'lgb'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-8-49d132e07ecf>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mlgb\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mLGBMClassifier\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[1;31m#第一步：学习率和迭代次数\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0msklearn\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmodel_selection\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mGridSearchCV\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0msklearn\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmodel_selection\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mtrain_test_split\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;31m# 切分数据 训练数据80% 验证数据20%\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'lgb'"
     ]
    }
   ],
   "source": [
    "from lightgbm import LGBMClassifier\n",
    "#第一步：学习率和迭代次数\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.model_selection import train_test_split\n",
    "# 切分数据 训练数据80% 验证数据20%\n",
    "# 为了加快速度CV选的3，其实一般用5\n",
    "# 因为每训练一次耗时很多，所以每个参数的选项不多，间隔比较大，正式的时候应该是比较多，间隔比较细的\n",
    "# 本次只是演示，所以如果最好参数位于区间的边缘也就直接用了，其实如果最好参数在边缘，需要重新再搜索。\n",
    "model = LGBMClassifier(boosting_type='gbdt',\n",
    "                       objective='binary',\n",
    "                       metrics='auc',\n",
    "                       learning_rate=0.1,\n",
    "                       max_depth=5,\n",
    "                       bagging_fraction=0.8,\n",
    "                       feature_fraction=0.8)\n",
    "parameters = {'n_estimators': [100, 150, 175, 200, 225, 250]}\n",
    "\n",
    "clf = GridSearchCV(model, parameters, cv=3, verbose=2)\n",
    "clf.fit(X_train, y_train)\n",
    "score_test = roc_auc_score(y_test, clf.predict_proba(X_test)[:, 1])\n",
    "\n",
    "print(\"LightGBM GridSearchCV AUC Score:   \", score_test)\n",
    "print(\"最优参数:\")\n",
    "print(clf.best_params_)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
